package com.tanhua.dubbo.api.db;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.dubbo.api.mapper.SettingsMapper;
import com.tanhua.model.db.Question;
import com.tanhua.model.db.Settings;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 通知设置服务接口实现类
 */
@DubboService
public class SettingsApiImpl implements SettingsApi{

    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * 根据当前用户id查询tb_settings表 获取3个通知设置值
     * @param userId
     * @return
     */
    @Override
    public Settings findSettingsById(Long userId) {
        QueryWrapper<Settings> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        return settingsMapper.selectOne(queryWrapper);
    }

    /**
     * 通知设置 - 保存
     *
     * @param settings
     */
    @Override
    public void addOrUpdateSettings(Settings settings) {
        //1根据用户id查询通知设置表记录是否存在
        Settings st = findSettingsById(settings.getUserId());
        //2如果记录存在 则更新
        if(st != null){
            settings.setId(st.getId());
            settingsMapper.updateById(settings);
        }else {
            //3如果记录不存在 则保存
            settingsMapper.insert(settings);
        }
    }


}
